//
//  YRJSaveTool.m
//  YanRenjie
//
//  Created by dllo on 15/11/6.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "YRJSaveTool.h"

@implementation YRJSaveTool

+ (void)isHaveFolder {
    // 1. 找沙盒路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    // 2. 拼接文件夹名
    NSString *myFile = [sandBoxPath stringByAppendingPathComponent:@"MySave"];
    // 3. 创建一个文件的管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    // 通过管理者调用方法, 根据路径判断文件夹是否存在
    if (![manager fileExistsAtPath:myFile]) {
        [manager createDirectoryAtPath:myFile withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSLog(@"%@", myFile);
}

+ (BOOL)isCityInPlist:(YRJAllHotCityModel *)city {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"hotCity.plist"];
    
    //反归档
    NSArray *cityArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    // 遍历
    for (YRJAllHotCityModel *tempModel in cityArr) {
        if ([tempModel.id isEqualToString:city.id]) {
            return NO;
        }
    }
    return YES;
}

+ (void)saveCityToPlist:(YRJAllHotCityModel *)city {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"hotCity.plist"];
    // 直接归档会覆盖掉之前的数据, 所以先进行反归档, 然后添加进之后再进行归档操作
    NSMutableArray *cityArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    if (cityArr == nil) {
        cityArr = [NSMutableArray array];
    }
    [cityArr addObject:city];
    // 归档
    [NSKeyedArchiver archiveRootObject:cityArr toFile:docPath];
    NSLog(@"%@", docPath);
}

+ (void)removeCityFromPlist:(YRJAllHotCityModel *)city {
    
}

+ (void)cancelCityInPlist:(YRJAllHotCityModel *)city {
    // 写路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"hotCity.plist"];
    // 反归档
    NSMutableArray *cityArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    // 遍历之后
    for (YRJAllHotCityModel *tempModel in cityArr) {
        if ([tempModel.id isEqualToString:tempModel.id]) {
            // 移除
            [cityArr removeObject:tempModel];
            break;
        }
    }
    // 归档
    [NSKeyedArchiver archiveRootObject:cityArr toFile:docPath];
}

+ (BOOL)isCountryInPlist:(YRJHotCountryModel *)country {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"hot.plist"];
    
    //反归档
    NSArray *countryArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    // 遍历
    for (YRJHotCountryModel *tempModel in countryArr) {
        if ([tempModel.id isEqual:country.id]) {
            return NO;
        }
    }
    return YES;
}

+ (BOOL)isInFile:(id)object type:(SaveToolType)type {
    // 写路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    switch (type) {
        case SaveToolCity: {
            // 拼接文件夹路径
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"hotCity.plist"];
            NSMutableArray *cityArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            YRJAllHotCityModel *model = object;
            //
            for (YRJAllHotCityModel *temp in cityArr) {
                if ([temp.id isEqualToString:model.id]) {
                    return NO;
                }
            }
        }
            break;
        case SaveToolCountry: {
            // 拼接文件夹路径
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"hot.plist"];
            NSMutableArray *countryArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            YRJHotCountryModel *model = object;
            //
            for (YRJHotCountryModel *temp in countryArr) {
                if ([temp.id isEqual:model.id]) {
                    return NO;
                }
            }
        }
    }
    return YES;
}

+ (void)saveCountryToPlist:(YRJHotCountryModel *)country {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"hot.plist"];
    // 直接归档会覆盖掉之前的数据, 所以先进行反归档, 然后添加进之后再进行归档操作
    NSMutableArray *countryArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    if (countryArr == nil) {
        countryArr = [NSMutableArray array];
    }
    [countryArr addObject:country];
    // 归档
    [NSKeyedArchiver archiveRootObject:countryArr toFile:docPath];
    NSLog(@"%@", docPath);
}

+ (void)removeCountryFromPlist:(YRJHotCountryModel *)country {
    
}

+ (void)cancelCountryInPlist:(YRJHotCountryModel *)country {
    // 写路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"hot.plist"];
    // 反归档
    NSMutableArray *countryArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    // 遍历之后
    for (YRJHotCountryModel *tempModel in countryArr) {
        if ([tempModel.id isEqual:tempModel.id]) {
            // 移除
            [countryArr removeObject:tempModel];
            break;
        }
    }
    // 归档
    [NSKeyedArchiver archiveRootObject:countryArr toFile:docPath];
}
@end
